﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NetPE.Disasm.x86;
using NetPE.Core.Metadata.Methods;
using System.IO;

namespace NetPE
{
    public partial class x86Disassembly : Form
    {
        public x86Disassembly()
        {
            InitializeComponent();
        }

        public void Disassemble(NativeMethodBody bdy)
        {
            InstructionCollection insts = Disassembler.Disassemble(bdy.Codes);

            uint offset = 0;
            BinaryReader rdr = new BinaryReader(new MemoryStream(bdy.Codes));
            foreach (Instruction inst in insts)
            {
                ListViewItem item = new ListViewItem();
                item.Text = "L_" + offset.ToString("x4").ToUpper();
                item.SubItems.Add(BitConverter.ToString(rdr.ReadBytes((int)inst.Length)).Replace('-', ' '));
                item.SubItems.Add(inst.Mnemonic);
                if (inst.Operand1 != null)
                    item.SubItems.Add(inst.Operand1.ToString());
                if (inst.Operand2 != null)
                    item.SubItems.Add(inst.Operand2.ToString());
                if (inst.Operand3 != null)
                    item.SubItems.Add(inst.Operand3.ToString());
                listView1.Items.Add(item);
                offset += inst.Length;
            }
        }
    }
}
